﻿import mx.events.EventDispatcher;

import com.mixmedia.mx.IEventDispatcher;
import com.mixmedia.mx.events.LoaderEvent;
import com.mixmedia.net.ILoadStrategy;
import mx.utils.Delegate;
/*
 *
 * @author
 * @version
 */


 class com.mixmedia.mx.XMLEvt extends XML implements IEventDispatcher,ILoadStrategy{
	private var iid:Number;
	private var isOpen:Boolean = false;
	private var isComplete:Boolean = false;

	public function XMLEvt(){
		EventDispatcher.initialize(this);
		ignoreWhite = true;
	};
	
	public function load(url:String):Void{
		super.load(url);
		clearInterval(iid);
		iid = setInterval(Delegate.create(this,checkStatus),250);
	}

	private function checkStatus():Void{
		var targetObj = this;
		if(this.getBytesTotal()>0 && isOpen!=true){
			dispatchEvent(new LoaderEvent(this,LoaderEvent.EVENTOPEN,targetObj));
			isOpen = true;
		}

		dispatchEvent(new LoaderEvent(this,LoaderEvent.EVENTPROGRESS,targetObj));
	}

	private function onLoad(success:Boolean):Void{
		super.onLoad(success);
		if(success==true){
			dispatchEvent(new LoaderEvent(this,LoaderEvent.EVENTREADY,this));			
		}else{
			dispatchEvent(new LoaderEvent(this,LoaderEvent.EVENTERROR,this,String(this.status)));
		};
	}

	private function onData(src:String):Void{
		super.onData(src);
		clearInterval(iid);
		dispatchEvent(new LoaderEvent(this,LoaderEvent.EVENTCOMPLETE,src));
	}

	public function getTarget() : Object {
		return this;
	}

	public function addEventListener(event:String, handler):Void{}
	private function dispatchEvent(eventObj:Object):Void{}
	public function removeEventListener(event:String, handler):Void{}
}